package com.example.shop_2207.fragment

import android.content.Context
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import androidx.recyclerview.widget.GridLayoutManager
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.ToastUtils
import com.bumptech.glide.Glide
import com.example.shop_2207.R
import com.example.shop_2207.adapter.CategoryAdapter
import com.example.shop_2207.adapter.GoodsAdapter
import com.example.shop_2207.databinding.FragmentHomeBinding
import com.example.shop_2207.entity.CategoryEntity
import com.example.shop_2207.entity.GoodsEntity
import com.example.shop_2207.mvvm.BaseFragment
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener
import com.youth.banner.loader.ImageLoader

/**
 * 搜索框---实现搜索功能
 * 轮播图--展示轮播数据
 * 分类---展示类别---切换商品
 * 商品列表--展示商品 --加载更多--刷新---点击跳转到详情界面
 */
class HomeFragment : BaseFragment<FragmentHomeBinding,HomeViewModel>() {
    val imglist = mutableListOf<String>()
    val catelist = mutableListOf<CategoryEntity>()
    lateinit var categoryAdapter: CategoryAdapter

    val goodslist = mutableListOf<GoodsEntity>()
    lateinit var goodsAdapter: GoodsAdapter

    var cateId = 14//加载Apple
    var page = 1//加载当前页码
    val size = 10
    override fun getLayout(): Int  = R.layout.fragment_home

    override fun getViewModel(): Class<HomeViewModel>  = HomeViewModel::class.java

    override fun initView() {
        //点击搜索框，跳转到搜索界面
        binding.etSearch.setOnClickListener {
            ARouter.getInstance().build("/app/SearchActivity").navigation()
        }


        //请求轮播图
        viewModel.banner()

        categoryAdapter = CategoryAdapter(catelist)
        binding.rvCate.adapter = categoryAdapter
        binding.rvCate.layoutManager = GridLayoutManager(context,5)
        //请求分类
        viewModel.cate()
        //分类点击事件
        categoryAdapter.setOnItemClickListener { adapter, view, position ->
            cateId = catelist[position].id
            page=1//进行页面切换时加载第一页数据
            viewModel.goods(cateId,page,size)
        }

        goodsAdapter = GoodsAdapter(goodslist)
        binding.rvGoods.adapter = goodsAdapter
        binding.rvGoods.layoutManager = GridLayoutManager(context,2)

        //请求商品
        viewModel.goods(cateId,page,size)

        //加载更多和刷新数据
        binding.smart.setOnRefreshLoadMoreListener(object:OnRefreshLoadMoreListener{
            override fun onRefresh(refreshLayout: RefreshLayout) {
                page=1
                viewModel.goods(cateId,page,size)
                binding.smart.finishRefresh()//结束刷新操作  不然会一直提示正在刷新
            }

            override fun onLoadMore(refreshLayout: RefreshLayout) {
                page++
                viewModel.goods(cateId,page,size)
                binding.smart.finishLoadMore()//结束加载更多操作，不然会一直正在加载
            }
        })
        goodsAdapter.addChildClickViewIds(R.id.addCar)
        goodsAdapter.setOnItemChildClickListener { adapter, view, position ->
            when(view.id){
                R.id.addCar->{
                    ToastUtils.showLong("点击了购物车$position")
                }
            }
        }
        /***
         * setOnItemClickListener点击的是整个布局
         * setOnItemChildClickListener点击的是布局中某一个具体的控件
         * 一定注意需要添加addChildClickViewIds
         */

        //点击item跳转到详情界面
        goodsAdapter.setOnItemClickListener { adapter, view, position ->
            ARouter.getInstance().build("/app/DetailActivity").withInt("goodsId",goodslist[position].id).navigation()
        }
        //传值为整个实体类时
      /*  goodsAdapter.setOnItemClickListener { adapter, view, position ->
            ARouter.getInstance().build("").withSerializable("goods",goodslist[position]).navigation()
        }*/


    }

    override fun initData() {
        //轮播图返回结果监听
        viewModel.bannerSuccess.observe(this){
            if(it.code==200){
                it.data.forEach {
                    imglist.add(it.imagePath)
                }
                binding.banner.setImages(imglist)
                binding.banner.setImageLoader(object :ImageLoader(){
                    override fun displayImage(
                        context: Context?,
                        path: Any?,
                        imageView: ImageView?
                    ) {
                        Glide.with(context!!).load(path).into(imageView!!)
                    }
                })
                binding.banner.start()
            }
        }
        viewModel.bannerFail.observe(this){
            ToastUtils.showLong(it)
        }
        //分类返回值监听
        viewModel.cateSuccess.observe(this){
            if(it.code==200){
               val result =  it.data[0].data
                if(result!=null){
                    catelist.addAll(result)
                }
                categoryAdapter.notifyDataSetChanged()
            }
        }
        viewModel.cateFail.observe(this){
            ToastUtils.showLong(it)
        }
        //商品返回值监听
        viewModel.goodsSuccess.observe(this){
            if(it.code==200){
                if(page==1){
                    goodslist.clear()
                }
                goodslist.addAll(it.data)
                goodsAdapter.notifyDataSetChanged()
            }
        }
        viewModel.goodsFail.observe(this){
            ToastUtils.showLong(it)
        }

    }

}